package com.itheima.web.controller.pms;

import com.itheima.service.cms.CmsSubjectProductRelationService;
import com.itheima.service.pms.*;
import com.itheima.utils.PageResult;
import com.itheima.utils.ResultData;
import com.itheima.vo.cms.CmsPrefrenceAreaProductRelation;
import com.itheima.vo.cms.CmsSubjectProductRelation;
import com.itheima.vo.pms.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("product")
@Slf4j
public class ProductController {

    @DubboReference
    private ProductSevice productSevice;

    @DubboReference
    public ProductCategoryService productCategoryService;

    @DubboReference
    private PmsBrandService brandService;

    @DubboReference
    private PmsMemberPriceService pmsMemberPriceService;

    @DubboReference
    private CmsPrefrenceAreaProductRelationService cpfaprService;

    @DubboReference
    private PmsProductAttributeValueService ppavService;

    @DubboReference
    private PmsProductFullReductionService ppfrService;

    @DubboReference
    private PmsProductLadderService pplService;

    @DubboReference
    private PmsSkuStockService pssService;

    @DubboReference
    private CmsSubjectProductRelationService csprSevice;


//    @DubboReference
//    private PmsM

    /**
     * 分页查询商品
     */
    @GetMapping("list")
    public ResultData list(Product product, int pageNum, int pageSize, String keyword) {

        //设置品牌属性
        if (product.getBrandId() != null) {
            Brand byId = brandService.getById(product.getBrandId());
            product.setBrandName(byId.getName());
        }
        product.setName(keyword);

        log.info("接收到数据：{}", product + " " + pageNum + " " + pageSize);

        long count = productSevice.countByStatus(product);

        pageNum = (pageNum - 1) * pageSize;

        //只查询未删除的商品
        List<Product> productList = productSevice.pageInfo(pageNum, pageSize, product);
        PageResult init = PageResult.init(count, (long) pageSize, (long) pageNum, productList);

        return ResultData.ok("操作成功", init);
    }

    /**
     * 根据商品名称或货号模糊查询
     */
    @GetMapping("simpleList")
    public ResultData simpleList(Product product) {
        Product product1 = productSevice.selectByKeyword(product.getName());
        return ResultData.ok("查询成功", product1);
    }

    /**
     *  创建商品
     */
    @PostMapping("create")
    @Transactional
    public ResultData create(@RequestBody ProductDto productDto) {
        //添加商品数据
        Product product=new Product();
        BeanUtils.copyProperties(productDto,product);
        boolean save = productSevice.save(product);
        if (!save){
            return ResultData.error("异常");
        }

        //添加分类数据
        //会员价目表 pms_member_price
        if (productDto.getMemberPriceList().size()!=0){
            for (PmsMemberPrice pmsMemberPrice : productDto.getMemberPriceList()) {
                pmsMemberPriceService.save(pmsMemberPrice);
            }
        }

        //查询偏好区域产品相关列表 cms_prefrence_area_product_relation
        if (productDto.getPrefrenceAreaProductRelationList().size()!=0){
            for (CmsPrefrenceAreaProductRelation cpapr : productDto.getPrefrenceAreaProductRelationList()) {
                cpfaprService.save(cpapr);
            }
        }

        //产品属性值列表  pms_product_attribute_value
        if (productDto.getProductAttributeValueList().size()!=0){
            for (PmsProductAttributeValue ppav : productDto.getProductAttributeValueList()) {
                ppavService.save(ppav);
            }
        }

        //产品完整减量清单 pms_product_full_reduction
        if (productDto.getProductFullReductionList().size()!=0){
            for (PmsProductFullReduction ppfr : productDto.getProductFullReductionList()) {
                ppfrService.save(ppfr);
            }
        }

        //产品阶梯列表 pms_product_ladder
        if (productDto.getProductLadderList().size()!=0){
            for (PmsProductLadder ppl : productDto.getProductLadderList()) {
                pplService.save(ppl);
            }
        }

        //库存清单 pms_sku_stock
        if (productDto.getSkuStockList().size()!=0){
            for (PmsSkuStock pss : productDto.getSkuStockList()) {
                pssService.save(pss);
            }
        }

        //主题产品关系列表  cms_subject_product_relation
        if (productDto.getSubjectProductRelationList().size()!=0){
            for (CmsSubjectProductRelation cspr : productDto.getSubjectProductRelationList()) {
                csprSevice.save(cspr);
            }
        }

        return ResultData.ok("添加成功", null);
    }

    /**
     * 批量修改删除状态
     */
    @PostMapping("/update/deleteStatus")
    public ResultData deleteStatus(Integer deleteStatus, @RequestParam("ids") List<Integer> ids) {
        productSevice.updateByIds(ids, deleteStatus);
        return ResultData.ok("修改成功", null);
    }

    /**
     * 批量设为新品
     */
    @PostMapping("/update/newStatus")
    public ResultData newStatus(@RequestParam("ids") List<Integer> ids, Integer newStatus) {
        productSevice.updateWithNewStatus(ids, newStatus);
        return ResultData.ok("修改成功", null);
    }

    /**
     * 批量上下架商品
     */
    @PostMapping("/update/publishStatus")
    public ResultData publishStatus(@RequestParam("ids") List<Integer> ids, Integer publishStatus) {
        productSevice.updateWithPublishStatus(ids, publishStatus);
        return ResultData.ok("修改成功", null);
    }

    /**
     * 批量推荐商品
     */
    @PostMapping("/update/recommendStatus")
    public ResultData recommendStatus(@RequestParam("ids") List<Integer> ids, Integer recommendStatus) {
        productSevice.updateWithreCommendStatus(ids, recommendStatus);
        return ResultData.ok("修改成功", null);
    }

    /**
     * 批量修改审核状态
     */
    @PostMapping("/update/verifyStatus")
    public ResultData verifyStatus(@RequestParam("ids") List<Integer> ids, Integer verifyStatus) {
        productSevice.updateWithreVerifyStatus(ids, verifyStatus);
        return ResultData.ok("修改成功", null);
    }

    /**
     * TODO 更新商品
     */
    @Transactional
    @PostMapping("/update/{id}")
    public ResultData update(@PathVariable Integer id,@RequestBody ProductDto productDto) {
        //修改商品数据
        Product product=new Product();
        BeanUtils.copyProperties(productDto,product);
        boolean b = productSevice.updateById(product);

        if (!b){
            return ResultData.error("修改失败");
        }

        //会员价目表 pms_member_price
        if (productDto.getMemberPriceList().size()!=0){
            for (PmsMemberPrice pmsMemberPrice : productDto.getMemberPriceList()) {
                pmsMemberPriceService.updateById(pmsMemberPrice);
            }
        }

        //查询偏好区域产品相关列表 cms_prefrence_area_product_relation
        if (productDto.getPrefrenceAreaProductRelationList().size()!=0){
            for (CmsPrefrenceAreaProductRelation cpapr : productDto.getPrefrenceAreaProductRelationList()) {
                cpfaprService.updateById(cpapr);
            }
        }

        //产品属性值列表  pms_product_attribute_value
        if (productDto.getProductAttributeValueList().size()!=0){
            for (PmsProductAttributeValue ppav : productDto.getProductAttributeValueList()) {
                ppavService.updateById(ppav);
            }
        }

        //产品完整减量清单 pms_product_full_reduction
        if (productDto.getProductFullReductionList().size()!=0){
            for (PmsProductFullReduction ppfr : productDto.getProductFullReductionList()) {
                ppfrService.updateById(ppfr);
            }
        }

        //产品阶梯列表 pms_product_ladder
        if (productDto.getProductLadderList().size()!=0){
            for (PmsProductLadder ppl : productDto.getProductLadderList()) {
                pplService.updateById(ppl);
            }
        }

        //库存清单 pms_sku_stock
        if (productDto.getSkuStockList().size()!=0){
            for (PmsSkuStock pss : productDto.getSkuStockList()) {
                pssService.updateById(pss);
            }
        }

        //主题产品关系列表  cms_subject_product_relation
        if (productDto.getSubjectProductRelationList().size()!=0){
            for (CmsSubjectProductRelation cspr : productDto.getSubjectProductRelationList()) {
                csprSevice.updateById(cspr);
            }
        }


        return ResultData.ok("操作成功", null);
    }

    /**
     * 根据商品id获取商品编辑信息
     */
    @GetMapping("updateInfo/{id}")
    @Transactional
    public ResultData updateInfo(@PathVariable Integer id) {
        Product product = productSevice.getBy(id);
        ProductDto productDto = new ProductDto();
        BeanUtils.copyProperties(product, productDto);
        //商品分类父id
        Long productCategoryId = product.getProductCategoryId();
        PmsProductCategory byId = productCategoryService.getById(productCategoryId);
        productDto.setCateParentId(byId.getParentId());

        //会员价目表 pms_member_price
        productDto.setMemberPriceList(pmsMemberPriceService.getByProductId(id));

        //查询偏好区域产品相关列表 cms_prefrence_area_product_relation
        productDto.setPrefrenceAreaProductRelationList(cpfaprService.getByProductId2(id));

        //产品属性值列表  pms_product_attribute_value
        productDto.setProductAttributeValueList(ppavService.getByProductId3(id));

        //产品完整减量清单 pms_product_full_reduction
        productDto.setProductFullReductionList(ppfrService.getByProductId4(id));

        //产品阶梯列表 pms_product_ladder
        productDto.setProductLadderList(pplService.getByProductId5(id));

        //库存清单 pms_sku_stock
        productDto.setSkuStockList(pssService.getByProductId(id));

        //主题产品关系列表  cms_subject_product_relation
        productDto.setSubjectProductRelationList(csprSevice.getByProductId(id));

        return ResultData.ok("操作成功", productDto);
    }

}
